﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Management;

namespace Member
{
    class Util
    {
        public static bool IsValidDateFormat(string dateFormat)
        {
            string[] formats = { "yyyy/mm/dd", "yyyy/m/d/", "yyyy/m/dd", "yyyy/m/d" };
            DateTime expectedDate;
            if (DateTime.TryParseExact(dateFormat, formats, new CultureInfo("en-US"), DateTimeStyles.None, out expectedDate))
            {
                return true;
            }
            return false;
        }

        public static DateTime convertStringtoDate(string date)
        {
            DateTime expectedDate;
            if(DateTime.TryParse(date, out expectedDate))
            {
                return DateTime.Parse(date);
            }
            return DateTime.Parse("1/1/1");
        }

        #region Get Hard Disk Serial
        public static string GetAllDiskDrives()
        {
            var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");

            foreach (ManagementObject wmi_HD in searcher.Get())
            {
                //HardDrive hd = new HardDrive();
                //hd.Model = wmi_HD["Model"].ToString();
                //hd.InterfaceType = wmi_HD["InterfaceType"].ToString();
                //hd.Caption = wmi_HD["Caption"].ToString();
                //hd.SerialNo = wmi_HD.GetPropertyValue("SerialNumber").ToString();//get the serailNumber of diskdrive
                //hdCollection.Add(hd);
                return wmi_HD.GetPropertyValue("SerialNumber").ToString();

            }
            return "";

        }

        /*public static string getSerial()
        {
            string res = ExecuteCommandSync("vol");
            const string search = "Number is";
            int startI = res.IndexOf(search, StringComparison.InvariantCultureIgnoreCase);

            if (startI > 0)
            {
                string currentDiskID = res.Substring(startI + search.Length).Trim();

                return currentDiskID;
            }
            return null;
        }

        private static string ExecuteCommandSync(object command)
        {
            try
            {
                // create the ProcessStartInfo using "cmd" as the program to be run,
                // and "/c " as the parameters.
                // Incidentally, /c tells cmd that we want it to execute the command that follows,
                // and then exit.
                System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);

                // The following commands are needed to redirect the standard output.
                // This means that it will be redirected to the Process.StandardOutput StreamReader.
                procStartInfo.RedirectStandardOutput = true;
                procStartInfo.UseShellExecute = false;
                // Do not create the black window.
                procStartInfo.CreateNoWindow = true;
                // Now we create a process, assign its ProcessStartInfo and start it
                System.Diagnostics.Process proc = new System.Diagnostics.Process();
                proc.StartInfo = procStartInfo;
                proc.Start();
                // Get the output into a string
                string result = proc.StandardOutput.ReadToEnd();
                // Display the command output.
                return result;
            }
            catch (Exception)
            {
                // Log the exception
                return null;
            }
        }*/
        #endregion
    }

}
